<template>
	<view class="update">
		<view class="update_main">
			<!-- <view class="update_main_item">
				<text>当前版本</text><text class="Vtext">V0.0.1</text>
			</view> -->
			<view class="update_main_item" @tap="update">
				<text>最新版本</text>
				<view style="display: flex;" class="Vtext">V{{appVersion}}
					<u-icon style="margin-left: 30rpx;" name="arrow-right" color="#bdbdbd" size="28"></u-icon>
				</view>
			</view>
			<view class="update_main_item">
				<text>更新日志</text><text class="Vtext"></text>
			</view>
			<view class="daily">
				<view class="">
					{{myVersion}}
				</view>
				<!-- <view class="">
					V23.01.14
				</view>
				<view class="">
					V23.01.14
				</view> -->
			</view>

			<u-modal :showCancelButton="true" @confirm="confirm" :show="show" :title="title">
				<block>
					<u-line-progress v-if="show" :percentage="temp" height="28" activeColor="#ff0000">

					</u-line-progress>
				</block>

			</u-modal>
		</view>

	</view>
</template>

<script>
import { version } from 'vue';
	var app = getApp();
	export default {
		data() {
			return {
				appVersion: "",
				flag: false,
				temp: 0,
				show: false,
				title: "下载中",
				isCancel: false,
				path: "",
				myVersion: "",
				getData: {}
			}
		},
		methods: {
			confirm() {
				this.show = false
			},
			cancel() {
				this.show = false
				this.isCancel = true
			},
			update() {
				let that = this;
				// console.log(this.$store.state.appVersion, uni.getStorageSync("appVersion"))
				var versionInfo = app.globalData.versionInfo;
				var localVersionInfo = app.globalData.localVersion;
				let serverVersion = versionInfo.appVersion;
				appVersion = serverVersion;
				this.myVersion = localVersionInfo['version'];
				
				let myVersion = this.myVersion;
				myVersion = myVersion.replace(/\./g, '')
				serverVersion = serverVersion.replace(/\./g, '')
				// console.log(myVersion);
				// console.log(version);
				if (myVersion == serverVersion) {
					uni.showToast({
						title: `已是最新版本！`,
						icon: "none"
					})
				} else {
					let token = uni.getStorageSync("token");

					uni.showModal({
						content: `当前最新版本为${serverVersionn},是否更新`,
						success({
							confirm
						}) {
							console.log(confirm)
							if (confirm) {
								const downloadTask = uni.downloadFile({
									url: that.getData.android, //todo 后面要区分ios和安卓
									header: {
										"X-Access-Token": token,
									},
									success: (res) => {
										if (res.statusCode === 200) {
											that.path = res.tempFilePath
											console.log('下载成功 ', res);
										}
									}
								});
								downloadTask.onProgressUpdate((res) => {
									that.flag = true
									that.show = true
									that.temp = res.progress
									if (res.progress == 100) {
										that.title = "下载完成!"
										that.flag = false
										that.show = false
										let ins = plus.runtime.install(
											plus.io.convertLocalFileSystemURL(that.path), {
												force: true
											},
											() => {
												uni.showToast({
													icon: 'none',
													title: '安装成功！'
												});
											}, (e) => {
												uni.showToast({
													icon: 'none',
													title: '安装失败！'
												});
											})

									}
									console.log('下载进度' + res.progress);
									console.log('已经下载的数据长度' + res.totalBytesWritten);
									console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
									if (that.isCancel) {
										downloadTask.abort();
										that.isCancel = false
									}
									// 满足测试条件，取消下载任务。
									if (res.progress > 50) {}
								});
							}
							// console.log(999)

						}
					})
				}

			}
		},
		onShow() {
			var localVersion = app.globalData.localVersion;
			var serverVersion = app.globalData.versionInfo;
			this.myVersion = localVersion['version'];
			this.appVersion = serverVersion['appVersion'];
			console.log('this.appVersion = ' + this.appVersion);
		}
	}
</script>

<style scoped lang="scss">
	page {
		background: #f1f1f1;
		padding: 18rpx 25rpx;
	}

	.update {
		background-color: #f1f1f1;
		height: 100vh;

		.update_main {
			border-radius: 20rpx;
			background-color: #fff;
			padding: 0 32rpx;

			.update_main_item {
				padding: 32rpx 0;
				border-bottom: 1rpx solid #f1f1f1;
				display: flex;
				justify-content: space-between;

				.Vtext {
					color: #BDBDBD;
				}

			}

			.daily {
				margin: 20rpx 0;

				view {
					padding: 10rpx 0;
					border-bottom: 1rpx solid #f1f1f1;
				}
			}
		}
	}
</style>
